package com.jinzhi.jzapi.certificate.controller;

import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.common.utils.ValidUtils;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.ExaminationGradVo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.WithZoreUtil;
import com.jinzhi.sms.support.SmsManager;
import com.jinzhi.sys.domain.UserDO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jinzhi.api.util.TokenTag.TOKEN_TAG;

@Slf4j
@RestController
@RequestMapping("/jzApi/assessor")
public class ApiAssessorController extends BaseController {

    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private PersonnelArrangementService personnelArrangementService;
    @Autowired
    private OrganizationNewsService organizationNewsService;
    @Autowired
    private ExamineeGroupService examineeGroupService;
    @Autowired
    private ExaminationQuestionsService examinationQuestionsService;
    @Autowired
    private ExaminationGradingService examinationGradingService;
    @Autowired
    private ExaminationSingleScoreService examinationSingleScoreService;
    @Autowired
    private DictService dictService;
    @Autowired
    private SmsManager smsSerevice;
    @Autowired
    private MessageplatfromService messageplatfromService;

    private static final String T_REG_SMS_PREFIX = "TRAINING_REG_";//培训端
    private static final String C_REG_SMS_PREFIX = "CERTIFICATE_REG_";//证书端
    private static final String O_REG_SMS_PREFIX = "ORTHER_REG_";//其他端

    /**
     * api小程序端-发送注册验证短信
     *
     * @Description: 调取短信平台发送短信，参数： 微信端口id，手机号码
     * @Param: [wxId, mobile]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-06-20
     */
    @PostMapping("/sendSms")
    @Log("api小程序端-发送注册验证短信")
    @ApiOperation(value = "api小程序端-发送注册验证短信", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> sendSms(@RequestHeader(value = TOKEN_TAG, required = false) String token,Integer wxId) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        //第一步验证手机号格式
        if (StringUtils.isBlank(user.getMobile())) {
            return ApiResult.lack("mobile");
        }
        if (!ValidUtils.isMobile(user.getMobile())) {
            return ApiResult.format("mobile");
        }
        //第二步发送短信，错误信息由方法直接返回
        Object smsDemo = EhCacheUtil.get("登录短信验证");
        if(smsDemo == null){
            MessageplatfromDO messageplatfromDO = messageplatfromService.findOneByKv("登录短信验证");
            if(messageplatfromDO != null){
                EhCacheUtil.put("登录短信验证",messageplatfromDO.getSmsDemo());
                if (wxId.equals(101)) {
                    smsSerevice.send(C_REG_SMS_PREFIX, user.getMobile(),messageplatfromDO.getSmsDemo());
                } else if (wxId.equals(102)) {
                    smsSerevice.send(T_REG_SMS_PREFIX, user.getMobile(),messageplatfromDO.getSmsDemo());
                } else {
                    smsSerevice.send(O_REG_SMS_PREFIX, user.getMobile(),messageplatfromDO.getSmsDemo());
                }
            }else{
                return ApiResult.fail("短信模板错误，请联系管理员！");
            }
        }else{
            if (wxId.equals(101)) {
                smsSerevice.send(C_REG_SMS_PREFIX, user.getMobile(),(String) smsDemo);
            } else if (wxId.equals(102)) {
                smsSerevice.send(T_REG_SMS_PREFIX, user.getMobile(),(String) smsDemo);
            } else {
                smsSerevice.send(O_REG_SMS_PREFIX, user.getMobile(),(String) smsDemo);
            }
        }

        return ApiResult.success("发送成功");
    }


    /**
     * api小程序端-校验验证码
     *
     * @Description: 校验短信验证码，验证成功后验证码失效，备用方法；参数：微信端口id，手机号码，验证码
     * @Param: [wxId, mobile, code]
     * @return: com.jinzhi.common.utils.ApiResult
     * @Author: kl
     * @Date: 2020-06-20
     */
    @PostMapping("/verify")
    @Log("api小程序端-校验验证码")
    @ApiOperation(value = "api小程序端-校验验证码", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult verify(@RequestHeader(value = TOKEN_TAG, required = false) String token,Integer wxId, String code) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        //错误信息由方法直接返回
        if (wxId.equals(101)) {
            smsSerevice.verify(C_REG_SMS_PREFIX, user.getMobile(), code);
        } else if (wxId.equals(102)) {
            smsSerevice.verify(T_REG_SMS_PREFIX, user.getMobile(), code);
        } else {
            smsSerevice.verify(O_REG_SMS_PREFIX, user.getMobile(), code);
        }

        EhCacheUtil.put(user.getId() + ":isLoginState", 1);
        return ApiResult.success("校验成功");
    }

    /**
     * api小程序端-判断是否需要验证进入接口
     *
     * @Description: 判断是否需要验证进入接口，参数：登录token
     * @Param: [wxId, mobile, code]
     * @return: com.jinzhi.common.utils.ApiResult
     * @Author: kl
     * @Date: 2020-06-20
     */
    @PostMapping("/verifyCode")
    @Log("api小程序端-校验验证码")
    @ApiOperation(value = "api小程序端-校验验证码", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult verifyCode(@RequestHeader(value = TOKEN_TAG, required = false) String token) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        int expNum = 3;
        //获取判断该考评员是否验证过
        Object expNums = EhCacheUtil.get("sexpNums");
        if(expNums == null){
            Map<String, String> map = new HashMap<>();
            map.put("name", "考评员验证最大次数");
            map.put("type", "exp_verify_num");
            String expVerifyNum = dictService.selDicBy(map);
            if (StringUtils.isNotBlank(expVerifyNum)) {
                expNum = Integer.parseInt(expVerifyNum);
            }
            EhCacheUtil.put("sexpNums", expNum);
        }

        //获取判断该考评员是否验证过
        Object isLoginState = EhCacheUtil.get(user.getId() + ":isLoginState");
        if(isLoginState == null){
            return ApiResult.success("请先验证！");
        }else{
            if((int)isLoginState > expNum){
                return ApiResult.success("请先验证！");
            }else{
                EhCacheUtil.put(user.getId() + ":isLoginState", (int)isLoginState+1);
                return ApiResult.success("验证通过！");
            }
        }
    }

    /**
     * api小程序端-获取当天的考试批次信息接口
     *
     * @Description: 获取当天的考试批次信息接口
     * @Param: [token]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-06-09
     */
    @GetMapping("/selThreeExabatch")
    @Log("api客户端-获取当天的考试批次信息接口")
    @ApiOperation("api客户端-获取当天的考试批次信息接口")
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> selThreeExabatch(@RequestHeader(value = TOKEN_TAG, required = false) String token) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        String roleName = getRoleNamesToken(user.getId());
        if (!roleName.contains("考评员")) {
            return ApiResult.fail("暂无权限！");
        }

        Map map = new HashMap();
        map.put("type", 1);
        map.put("days", 0);
        List<ExaminationBatchDO> examinationBatchDOS = examinationBatchService.selListExamBatch(map);
        return ApiResult.success(examinationBatchDOS);
    }

    /**
     * api客户端-获取对应考点信息接口
     *
     * @Description: 获取考评员对应考点的信息，考场序号、工位序号（1,2,3；4,5,6；根据教室设计的工位数划分），返回供考评员选择；这里需要判断用户身份！;
     * 参数 ：验证token，认证批次id，考试批次id
     * @Param: [token, cbatchId, ebatchId]
     * @return: com.jinzhi.common.utils.ApiResult<?>
     * @Author: kl
     * @Date: 2020-06-09
     */
    @GetMapping("/selExamPoint")
    @Log("api客户端-获取对应考点信息接口")
    @ApiOperation("api客户端-获取对应考点信息接口")
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> selExamPoint(@RequestHeader(value = TOKEN_TAG, required = false) String token, String cbatchId, String ebatchId) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        Map map = new HashMap();
        String roleName = getRoleNamesToken(user.getId());
        if (!roleName.contains("考评员")) {
            return ApiResult.fail("暂无权限！");
        }
        //保存用户id至缓存，为后面回调提供
        EhCacheUtil.put(user.getId() + ":cbatchId", Long.parseLong(cbatchId));
        EhCacheUtil.put(user.getId() + ":ebatchId", Long.parseLong(ebatchId));

        PersonnelArrangementDO personnelArrangementDO = personnelArrangementService.findOneByKv("user_id", user.getId(), "cbatch_id", Long.parseLong(cbatchId));
        if (personnelArrangementDO == null) {
            return ApiResult.fail("暂未安排，请联系考点管理员!");
        }
        map.put("orgId", personnelArrangementDO.getOrgId());

        OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(personnelArrangementDO.getOrgId());
        if (organizationNewsDO != null) {
            map.put("orgName", organizationNewsDO.getName());
        } else {
            map.put("orgName", "考点已删除！");
        }

        Map map1 = new HashMap();
        map1.put("orgId", personnelArrangementDO.getOrgId());
        map1.put("ebatchId", Long.parseLong(ebatchId));
        List<String> roomNums = examineeGroupService.selRoomNum(map1);
        if (roomNums != null) {
            map.put("roomNum", roomNums);
        } else {
            map.put("roomNum", "暂无考场！");
        }

        List<String> stationNums = new ArrayList<>();
        stationNums.add("1,2,3");
        stationNums.add("4,5,6");
        map.put("stationNum", stationNums);

        return ApiResult.success(map);
    }

    @GetMapping("/informVerify")
    @Log("api客户端-验证考评信息接口")
    @ApiOperation("api客户端-验证考评信息接口")
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> informVerify(@RequestHeader(value = TOKEN_TAG, required = false) String token, String orgId, String roomNum, String stationNum) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        Map reMap = new HashMap();
        reMap.put("orgId", orgId);
        reMap.put("roomNum", roomNum);
        reMap.put("stationNum", stationNum);

        //获取缓存的认证批次id和考试批次id
        Long cbatchId = (Long) EhCacheUtil.get(user.getId() + ":cbatchId");
        if (cbatchId != null) {
            //查询考评员资格
            PersonnelArrangementDO personnelArrangementDO = personnelArrangementService.findOneByKv("user_id", user.getId(), "cbatch_id", cbatchId);
            if (personnelArrangementDO == null) {
                return ApiResult.fail("暂未安排，请联系考点管理员!");
            }
        } else {
            return ApiResult.success("缓存报错，请重试！");
        }

        Long ebatchId = (Long) EhCacheUtil.get(user.getId() + ":ebatchId");
        if (ebatchId != null) {
            Map map = new HashMap();
            map.put("ebatchId", ebatchId);
            map.put("enterExpertId", user.getId());
            ExamineeGroupDO examineeGroupDO = examineeGroupService.selSingleNums(map);
            if (examineeGroupDO != null) {
                if (examineeGroupDO.getGroupNum() == null) {
                    reMap.put("groupNum", 1);
                } else {
                    reMap.put("groupNum", Integer.parseInt(examineeGroupDO.getGroupNum()) + 1);//最大组号+1，为新一组考生的组号
                }
            } else {
                reMap.put("groupNum", 1);
            }

            //查询考试批次，获得考试批次开始时间并返回
            ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(ebatchId);
            if (examinationBatchDO != null) {
                reMap.put("startingTime", examinationBatchDO.getStartTime());
            } else {
                return ApiResult.fail("考试批次信息有误，请重试或联系管理员！");
            }
        } else {
            return ApiResult.success("缓存报错，请重试！");
        }

        //保存用户id至缓存，为后面回调提供
        EhCacheUtil.put(user.getId() + ":roomNum", roomNum);
        EhCacheUtil.put(user.getId() + ":stationNum", stationNum);
        EhCacheUtil.put(user.getId() + ":orgId", Long.parseLong(orgId));
        return ApiResult.success(reMap);
    }

    @GetMapping("/selStuList")
    @Log("api客户端-获取考生信息接口")
    @ApiOperation("api客户端-获取考生信息接口")
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> selStuList(@RequestHeader(value = TOKEN_TAG, required = false) String token, String groupNum) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        Long ebatchId = (Long) EhCacheUtil.get(user.getId() + ":ebatchId");
        if (ebatchId != null) {
            Long orgId = (Long) EhCacheUtil.get(user.getId() + ":orgId");
            if (orgId != null) {
                String roomNum = (String) EhCacheUtil.get(user.getId() + ":roomNum");
                if (roomNum != null) {
                    //获取缓存中的考评员测评工位号
                    String stationNum = (String) EhCacheUtil.get(user.getId() + ":stationNum");
                    if (stationNum != null) {
                        Map map = new HashMap();
                        map.put("orgId", orgId);
                        map.put("ebatchId", ebatchId);
                        map.put("examinRoomNum", roomNum);
                        groupNum = WithZoreUtil.frontCompWithZore(Integer.parseInt(groupNum), 2);
                        map.put("groupNum", groupNum);
                        List list = new ArrayList();
                        if (stationNum.equals("1,2,3")) {
                            list.add("01");
                            list.add("02");
                            list.add("03");
                            map.put("stationNum", list);
                        } else {
                            list.add("04");
                            list.add("05");
                            list.add("06");
                            map.put("stationNum", list);
                        }

                        List<Map> remm = examineeGroupService.selStuInfroms(map);
                        return ApiResult.success(remm);
                    } else {
                        return ApiResult.success("缓存报错，请重试！");
                    }
                } else {
                    return ApiResult.success("缓存报错，请重试！");
                }
            } else {
                return ApiResult.success("缓存报错，请重试！");
            }
        } else {
            return ApiResult.success("缓存报错，请重试！");
        }

    }

    @GetMapping("/selRatingTable")
    @Log("api客户端-获取评分表信息接口")
    @ApiOperation("api客户端-获取评分表信息接口")
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> selRatingTable(@RequestHeader(value = TOKEN_TAG, required = false) String token, String groupNum, String number) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        Map reMap = new HashMap();
        reMap.put("groupNum", groupNum);
        reMap.put("number", number);
        Long ebatchId = (Long) EhCacheUtil.get(user.getId() + ":ebatchId");
        if (ebatchId != null) {
            Long orgId = (Long) EhCacheUtil.get(user.getId() + ":orgId");
            if (orgId != null) {
                String roomNum = (String) EhCacheUtil.get(user.getId() + ":roomNum");
                if (roomNum != null) {
                    //获取缓存中的考评员测评工位号
                    String stationNum = (String) EhCacheUtil.get(user.getId() + ":stationNum");
                    if (stationNum != null) {
                        //考生部分
                        Map map = new HashMap();
                        map.put("orgId", orgId);
                        map.put("ebatchId", ebatchId);
                        map.put("examinRoomNum", roomNum);
                        groupNum = WithZoreUtil.frontCompWithZore(Integer.parseInt(groupNum), 2);
                        map.put("groupNum", groupNum);
                        List list = new ArrayList();
                        if (stationNum.equals("1,2,3")) {
                            list.add("01");
                            list.add("02");
                            list.add("03");
                            map.put("stationNum", list);
                        } else {
                            list.add("04");
                            list.add("05");
                            list.add("06");
                            map.put("stationNum", list);
                        }

                        List<Map> remm = examineeGroupService.selStuInfroms(map);
                        reMap.put("stuList", remm);

                        //评分表部分
                        String stations;
                        if (stationNum.equals("1,2,3")) {
                            stations = "01";
                        } else {
                            stations = "04";
                        }
                        ExamineeGroupDO examineeGroupDO = examineeGroupService
                                .findOneByKv("org_id", orgId, "ebatch_id", ebatchId, "examin_room_num", roomNum, "group_num", groupNum, "station_num", stations);
                        if (examineeGroupDO != null) {
                            ExaminationQuestionsDO examinationQuestionsDO = examinationQuestionsService.findOneByKv("epaper_id", examineeGroupDO.getEpaperId(), "title_number", number, "type", 3);
                            if (examinationQuestionsDO != null) {
                                Map mapcc = new HashMap();
                                mapcc.put("topicContent", examinationQuestionsDO.getIntroduce());
                                mapcc.put("topicAnalysis", examinationQuestionsDO.getOptions());
                                List<ExaminationGradVo> examinationGradVos = examinationGradingService.selExaminationGradList(mapcc);
                                reMap.put("examGradingList", examinationGradVos);
                            } else {
                                return ApiResult.fail("信息错误，请重试或联系管理员！");
                            }
                        } else {
                            return ApiResult.fail("信息错误，请重试或联系管理员！");
                        }
                    } else {
                        return ApiResult.success("缓存报错，请重试！");
                    }
                } else {
                    return ApiResult.success("缓存报错，请重试！");
                }
            } else {
                return ApiResult.success("缓存报错，请重试！");
            }
        } else {
            return ApiResult.success("缓存报错，请重试！");
        }

        return ApiResult.success(reMap);
    }

    /** 
    * 分数提交接口
    * 
    * @Description: 考评员分数提交接口，参数：验证token，组号，考生一状态（0正常，1缺考），考生一打分情况集合，考生二状态（0正常，1缺考），考生二打分情况集合，考生三状态（0正常，1缺考），考生三打分情况集合
    * @Param: [token, groupNum, oneState, stuOneList, twoState, stuTwoList, thereState, stuThreeList] 
    * @return: com.jinzhi.common.utils.ApiResult<?> 
    * @Author: kl
    * @Date: 2020-06-10
    */ 
    @PostMapping("/saveScore")
    @Log("api客户端-分数提交接口")
    @ApiOperation("api客户端-分数提交接口")
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public ApiResult<?> saveScore(@RequestHeader(value = TOKEN_TAG, required = false) String token, String groupNum, String oneState, String stuOneList, String twoState, String stuTwoList,
                                  String thereState, String stuThreeList) {
        //1.根据token获取用户登录数据
        UserDO user = this.getClientUser(token);
        if (null == user) {
            return ApiResult.invalid();
        }

        Long ebatchId = (Long) EhCacheUtil.get(user.getId() + ":ebatchId");
        if (ebatchId != null) {
            Long orgId = (Long) EhCacheUtil.get(user.getId() + ":orgId");
            if (orgId != null) {
                String roomNum = (String) EhCacheUtil.get(user.getId() + ":roomNum");
                if (roomNum != null) {
                    //获取缓存中的考评员测评工位号
                    String stationNum = (String) EhCacheUtil.get(user.getId() + ":stationNum");
                    if (stationNum != null) {
                        Map map = new HashMap();
                        map.put("orgId", orgId);
                        map.put("ebatchId", ebatchId);
                        map.put("examinRoomNum", roomNum);
                        groupNum = WithZoreUtil.frontCompWithZore(Integer.parseInt(groupNum), 2);
                        map.put("groupNum", groupNum);
                        List list = new ArrayList();
                        if (stationNum.equals("1,2,3")) {
                            list.add("01");
                            list.add("02");
                            list.add("03");
                            map.put("stationNum", list);
                        } else {
                            list.add("04");
                            list.add("05");
                            list.add("06");
                            map.put("stationNum", list);
                        }

                        List<Map> remm = examineeGroupService.selStuInfroms(map);

                        if (remm != null && remm.size() > 0) {
                            List<ExaminationSingleScoreDO> examinationSingleScoreDOS = new ArrayList<>();
                            if (StringUtils.isNotBlank(oneState)) {
                                if (StringUtils.isNotBlank(stuOneList)) {
                                    JSONArray jsonArray = JSONArray.fromObject(stuOneList);
                                    ExaminationGradingDO examinationGradingDO = examinationGradingService.selectById(jsonArray.getJSONObject(0).getString("id"));
                                    if (examinationGradingDO != null) {
                                        EhCacheUtil.put(user.getId() + ":ibankId", examinationGradingDO.getIbankId());//上传缓存
                                    }
                                    //根据考试批次、考生、第一项评分标准id、录入考评员id判断该生的该题是否已录入过分数
                                    ExaminationSingleScoreDO examinationSingleScoreDO1 = examinationSingleScoreService
                                            .findOneByKv("stu_sign_id", Long.parseLong(remm.get(0).get("stuId").toString()), "ebatch_id", ebatchId,
                                                    "egrading_id", jsonArray.getJSONObject(0).getString("id"), "enter_expert_id", user.getId());
                                    if (examinationSingleScoreDO1 != null) {
                                        return ApiResult.fail("该题评分信息已提交过，请继续下一题评分！");
                                    } else {
                                        for (int i = 0; i < jsonArray.size(); i++) {
                                            ExaminationSingleScoreDO examinationSingleScoreDO = new ExaminationSingleScoreDO();
                                            examinationSingleScoreDO.setEbatchId(ebatchId);//考试批次id
                                            examinationSingleScoreDO.setEgradingId(Long.parseLong(jsonArray.getJSONObject(i).getString("id")));//评分标准id
                                            if (oneState.equals("0")) {
                                                if (StringUtils.isBlank(jsonArray.getJSONObject(i).getString("value"))) {
                                                    examinationSingleScoreDO.setScore(0);//分数
                                                } else {
                                                    examinationSingleScoreDO.setScore(Integer.parseInt(jsonArray.getJSONObject(i).getString("value")));//分数
                                                }
                                            } else {
                                                examinationSingleScoreDO.setScore(0);//考生缺考，分数直接为0
                                            }
                                            if (examinationGradingDO != null) {
                                                examinationSingleScoreDO.setIbankId(examinationGradingDO.getIbankId());//题库表id
                                            }
                                            examinationSingleScoreDO.setStuSignId(Long.parseLong(remm.get(0).get("stuId").toString()));//考生id
                                            examinationSingleScoreDO.setEnterExpertId(user.getId());//录入考评员id
                                            examinationSingleScoreDO.setUpdaterId(user.getId());//修改人id
                                            examinationSingleScoreDOS.add(examinationSingleScoreDO);
                                        }
                                    }
                                }
                            }

                            if(remm.size() >= 2){
                                if (StringUtils.isNotBlank(twoState)) {
                                    if (StringUtils.isNotBlank(stuTwoList)) {
                                        JSONArray jsonArray1 = JSONArray.fromObject(stuTwoList);
                                        //根据考试批次、考生、第一项评分标准id、录入考评员id判断该生的该题是否已录入过分数
                                        ExaminationSingleScoreDO examinationSingleScoreDO1 = examinationSingleScoreService
                                                .findOneByKv("stu_sign_id", Long.parseLong(remm.get(0).get("stuId").toString()), "ebatch_id", ebatchId,
                                                        "egrading_id", jsonArray1.getJSONObject(0).getString("id"), "enter_expert_id", user.getId());
                                        if (examinationSingleScoreDO1 != null) {
                                            return ApiResult.fail("该题评分信息已提交过，请继续下一题评分！");
                                        } else {
                                            for (int i = 0; i < jsonArray1.size(); i++) {
                                                ExaminationSingleScoreDO examinationSingleScoreDO = new ExaminationSingleScoreDO();
                                                examinationSingleScoreDO.setEbatchId(ebatchId);//考试批次id
                                                examinationSingleScoreDO.setEgradingId(Long.parseLong(jsonArray1.getJSONObject(i).getString("id")));//评分标准id
                                                if (twoState.equals("0")) {
                                                    if (StringUtils.isBlank(jsonArray1.getJSONObject(i).getString("value"))) {
                                                        examinationSingleScoreDO.setScore(0);//分数
                                                    } else {
                                                        examinationSingleScoreDO.setScore(Integer.parseInt(jsonArray1.getJSONObject(i).getString("value")));//分数
                                                    }
                                                } else {
                                                    examinationSingleScoreDO.setScore(0);//考生缺考，分数直接为0
                                                }

                                                Long ibankId = (Long) EhCacheUtil.get(user.getId() + ":ibankId");
                                                if (ibankId != null) {
                                                    examinationSingleScoreDO.setIbankId(ibankId);//题库表id
                                                }
                                                examinationSingleScoreDO.setStuSignId(Long.parseLong(remm.get(1).get("stuId").toString()));//考生id
                                                examinationSingleScoreDO.setEnterExpertId(user.getId());//录入考评员id
                                                examinationSingleScoreDO.setUpdaterId(user.getId());//修改人id
                                                examinationSingleScoreDOS.add(examinationSingleScoreDO);
                                            }
                                        }
                                    }
                                }
                            }

                            if(remm.size() >= 3){
                                if (StringUtils.isNotBlank(thereState)) {
                                    if (StringUtils.isNotBlank(stuThreeList)) {
                                        JSONArray jsonArray2 = JSONArray.fromObject(stuThreeList);
                                        //根据考试批次、考生、第一项评分标准id、录入考评员id判断该生的该题是否已录入过分数
                                        ExaminationSingleScoreDO examinationSingleScoreDO1 = examinationSingleScoreService
                                                .findOneByKv("stu_sign_id", Long.parseLong(remm.get(0).get("stuId").toString()), "ebatch_id", ebatchId,
                                                        "egrading_id", jsonArray2.getJSONObject(0).getString("id"), "enter_expert_id", user.getId());
                                        if (examinationSingleScoreDO1 != null) {
                                            return ApiResult.fail("该题评分信息已提交过，请继续下一题评分！");
                                        } else {
                                            for (int i = 0; i < jsonArray2.size(); i++) {
                                                ExaminationSingleScoreDO examinationSingleScoreDO = new ExaminationSingleScoreDO();
                                                examinationSingleScoreDO.setEbatchId(ebatchId);//考试批次id
                                                examinationSingleScoreDO.setEgradingId(Long.parseLong(jsonArray2.getJSONObject(i).getString("id")));//评分标准id
                                                if (thereState.equals("0")) {
                                                    if (StringUtils.isBlank(jsonArray2.getJSONObject(i).getString("value"))) {
                                                        examinationSingleScoreDO.setScore(0);//分数
                                                    } else {
                                                        examinationSingleScoreDO.setScore(Integer.parseInt(jsonArray2.getJSONObject(i).getString("value")));//分数
                                                    }
                                                } else {
                                                    examinationSingleScoreDO.setScore(0);//考生缺考，分数直接为0
                                                }
                                                Long ibankId = (Long) EhCacheUtil.get(user.getId() + ":ibankId");
                                                if (ibankId != null) {
                                                    examinationSingleScoreDO.setIbankId(ibankId);//题库表id
                                                }
                                                examinationSingleScoreDO.setStuSignId(Long.parseLong(remm.get(2).get("stuId").toString()));//考生id
                                                examinationSingleScoreDO.setEnterExpertId(user.getId());//录入考评员id
                                                examinationSingleScoreDO.setUpdaterId(user.getId());//修改人id
                                                examinationSingleScoreDOS.add(examinationSingleScoreDO);
                                            }
                                        }
                                    }
                                }
                            }

                            if (examinationSingleScoreDOS.size() > 0) {
                                boolean b = examinationSingleScoreService.insertBatch(examinationSingleScoreDOS, examinationSingleScoreDOS.size());
                                if (!b) {
                                    return ApiResult.fail("提交错误，请重试！");
                                }
                            }

                        } else {
                            return ApiResult.fail("考生信息错误，请重试！");
                        }
                    } else {
                        return ApiResult.success("缓存报错，请重试！");
                    }
                } else {
                    return ApiResult.success("缓存报错，请重试！");
                }
            } else {
                return ApiResult.success("缓存报错，请重试！");
            }
        } else {
            return ApiResult.success("缓存报错，请重试！");
        }

        return ApiResult.success();
    }
}
